goog.provide('czk.ui.Window');

goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.ui.Dialog');

goog.require('czk.ui.ZKComponent');

/**
 * @constructor
 * @extends {czk.ui.ZKComponent}
 */
czk.ui.Window = function() {
  goog.base(this);
};
goog.inherits(czk.ui.Window, czk.ui.ZKComponent);

czk.ui.Window.prototype.mode = null;
czk.ui.Window.prototype.dialog = null;

czk.ui.Window.prototype.createDom = function() {
  var dh = this.getDomHelper();
  if (!this.className)
    this.className = 'lbox';
  var el = dh.createDom(goog.dom.TagName.DIV, {
    'className': this.className
  });
  this.setElementInternal(el);
  if (this.mode == 'modal') {
    this.dialog = new goog.ui.Dialog();
    this.dialog.setVisible(true);
    this.dialog.setButtonSet(null);
    return;
  }

};

/** @inheritDoc */
czk.ui.Window.prototype.enterDocument =
    function() {
      goog.base(this, 'enterDocument');
      if (this.dialog != null) {
        this.getHandler().listen(this.dialog,
            goog.ui.Dialog.EventType.AFTER_HIDE, this.closeDialog, false, this);
      }
    };

czk.ui.Window.prototype.closeDialog = function() {
  this.portlet.sendXhr(this, 'onClose');
  var parent = this.getParent();
  parent.removeChild(this, true);
};

czk.ui.Window.prototype.getContentElement = function() {
  if (this.mode == 'modal')
    return this.dialog.getContentElement();
  return goog.base(this, 'getContentElement');
};

czk.ui.Window.prototype.initialize =
    function(name, parameter, portlet, children) {
      goog.base(this, 'initialize', name, parameter, portlet, children);
      if (parameter) {
        if (parameter['mode'])
          this.mode = parameter['mode'];
      }
    };

czk.ui.Window.prototype.disposeInternal = function() {
  goog.base(this, 'disposeInternal');
  this.dialog.dispose();
  this.dialog = null;
};

czk.ui.Window.create = function() {
  return new czk.ui.Window();
};
